<template>
  <div>
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :beforeClose="handleClose"
      :visible.sync="dialogVisible"
      width="500px">
      <el-form :model="sendForm" size="mini" label-width="130px" :rules="rules" ref="sendFormRef">
        <el-form-item label="统一社会信用代码：" prop="businessCode">
          <el-input v-model="sendForm.businessCode" @blur="seachRepeat" clearable style="width:200px"></el-input>
        </el-form-item>

         <el-form-item label="客户名称：" prop="customerName">
            <el-input v-model="sendForm.customerName" style="width:200px"></el-input>
         </el-form-item>

        <el-form-item label="客户性质：">
          <el-select v-model="sendForm.customerQuality">
            <el-option v-for="item in customerQualityList"  :key="item.cddId"  :value="item.cddId" :label="item.cddValue"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="客户规模：">
          <el-select v-model="sendForm.newScale" clearable>
            <el-option v-for="(item,index) in newScaleList" :key="index" :value="item" :label="item"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="规模性质：">
          <el-select v-model="sendForm.newNature" clearable>
            <el-option v-for="(item,index) in newNatureList" :key="index" :value="item" :label="item"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="月用量(吨)：">
          <el-input type="number" v-model="sendForm.aboutBuyNum" style="width:200px" clearable></el-input>
        </el-form-item>

        <el-form-item label="预估吨毛利(元)：">
          <el-input type="number" v-model="sendForm.aboutPerProfit" style="width:200px" clearable></el-input>
        </el-form-item>

        <el-form-item label="客户协助人：">
          <el-select v-model="sendForm.subChargeIds" filterable placeholder="请选择" clearable multiple>
            <el-option
              v-for="item in userOptionList"
              :key="item.userId"
              :label="item.userName"
              :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="handleConfirm" :loading="btnLoading">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getCustomerSetting,
  checkBusinessCode,
  getCompanyInfo,
  getOrderUsersList,
} from '@/api/partner';
import {
  getCustomerInfoAPI,
} from '@/api/company';
import { validatePhone } from '@/utils/validate';
import {addSonCustomerAPI} from '@/api/fms';
const defaultsendForm = {
  parentCustomerId:'',
  businessCode:'',//社会统一信用码
  customerName: '',//客户名称
  customerQuality:'',//客户性质
  newScale:'',//客户规模
  newNature:'',//规模性质
  aboutBuyNum:'',//月使用量
  aboutPerProfit: '',//吨毛利
  subChargeIds:[],//客户协助人
};
export default {
  name: "addSonCustomer",
  data() {
    return {
      sendForm:JSON.parse(JSON.stringify(defaultsendForm)),
      title: '',
      dialogVisible: false,
      btnLoading: false,
      customerQualityList: [],
      newScaleList: [],
      newNatureList: [],
      userOptionList:[],
      rules:{
        businessCode:[
          { required: true, message: '请输入统一社会信用代码', trigger: 'blur' }
        ],
        customerName:[
          { required: true, message: '请输入客户名称', trigger: 'blur' },
        ],
      },
    }
  },
  filters:{

  },
  created() {
  },
  methods: {
    //获取客户配置
    getCustomerSettingList() {
      getCustomerSetting().then((res) => {
        res.forEach((el) => {
         if (el.cdtCode == 'customer_quality') {//客户性质
            this.customerQualityList = el.data;
          }
        });
      })
    },

    getCustomerInfo() {
      getCustomerInfoAPI({tab:1}).then((res) => {
        this.newScaleList=res;
      })
      getCustomerInfoAPI({tab:2}).then((res) => {
        this.newNatureList=res;
      })
    },

    //获取员工下拉选项
    getUserOptionListData(){
      getOrderUsersList({
        status:1
      }).then((res)=>{
        this.userOptionList = res;
      })
    },

    seachRepeat() {
      if (this.sendForm.businessCode) {
        checkBusinessCode({
          businessCode: this.sendForm.businessCode,
          customerId: this.sendForm.customerId ? this.sendForm.customerId : 0
        }).then((res) => {
          getCompanyInfo({
            businessCode: this.sendForm.businessCode
          }).then((companyInfo) => {
            this.sendForm.businessCode = companyInfo.businessCode;
            this.sendForm.customerName = companyInfo.companyName;
          })
        }).catch((rej) => {
          if (rej.code == 10001) {
            this.$alert(rej.msg, '提示', {
              confirmButtonText: '确定',
            });
            this.sendForm.businessCode = null;
          }
        })
      }
    },

    //显示编辑/新增产品
    showPop(type, item) {
      this.title = type == 'add' ? '新增子客户' :  '编辑子客户';
      this.dialogVisible = true;
      this.getCustomerSettingList();
      this.getCustomerInfo();
      this.getUserOptionListData();
      if (type == 'add') {
        this.sendForm = JSON.parse(JSON.stringify(defaultsendForm));
        this.sendForm.parentCustomerId = item.customerId;
      }
      // else {
      //   for (let key in item) {
      //     this.sendForm[key]= item[key]? item[key]: defaultsendForm[key]
      //   }
      //   this.sendForm.isBuyPermission = +item.isBuyPermission;
      //   this.sendForm.isMainContact = +item.isMainContact;
      // }
    },
    //关闭子客户
    handleClose() {
      this.$refs.sendFormRef.clearValidate();
      this.dialogVisible = false;
      this.btnLoading = false;
    },

    //提交子客户
    handleConfirm() {
      this.$refs.sendFormRef.validate((valid) => {
        if (valid) {
          this.btnLoading = true;
          const data = JSON.parse(JSON.stringify(this.sendForm))
          data.subChargeIds=data.subChargeIds.join(',')
          addSonCustomerAPI(data).then(() => {
            this.btnLoading = false;
            this.$message.success('操作成功')
            this.handleClose();
            this.$emit('update')
          }).catch(() => {
            this.btnLoading = false;
          })
        }
      });
    },

  }
};
</script>

<style scoped lang="scss">

</style>
